package com.ruoyi.common.utils;

import java.io.File;
import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import com.sun.mail.util.MailSSLSocketFactory;

public class EmailUtil {
	public static void main(String[] args) {
		MailFromUserModel fromUser = new MailFromUserModel("smtp.163.com", "shixiangwen03@163.com", "mnb2009");
		MailToUserModel toUser = new MailToUserModel("114476513@qq.com", "这是标题", "这是内容", null, null);
		System.out.println(sendSMTPEmail(fromUser, toUser));
	}

	/**
	 * 使用smtp邮件协议发送邮件
	 *
	 * @param fromUser
	 * @param toUser
	 * @return
	 */
	public static SendEmailResult sendSMTPEmail(MailFromUserModel fromUser, MailToUserModel toUser) {
		try {
			// 1.设置邮件session的属性配置
			Properties properties = new Properties();
			properties.setProperty("mail.debug", "true");// 开启debug调试，以便在控制台查看
			properties.setProperty("mail.smtp.auth", "true");// 发送服务器需要身份验证
			properties.setProperty("mail.transport.protocol", "smtp");// 发送邮件协议名称
			// 开启SSL加密，否则会失败
			MailSSLSocketFactory sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
			properties.put("mail.smtp.ssl.enable", "true");
			properties.put("mail.smtp.ssl.socketFactory", sf);

			// 2.创建session
			Session session = Session.getInstance(properties);
			Transport ts = session.getTransport();
			ts.connect(fromUser.getHost(), fromUser.getUser(), fromUser.getPassWord());

			// 3.创建邮件对象
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(fromUser.getUser()));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(toUser.getUser()));
			// 3.1设置邮件标题
			message.setSubject(toUser.getSubject());
			// 3.2设置邮件主体
			Multipart multipart = new MimeMultipart();
			// 3.3设置邮件消息内容
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(toUser.getText());
			multipart.addBodyPart(messageBodyPart);
			// 3.4邮件主体设置附件
			if (toUser.getFile() != null) {
				if (!toUser.getFile().exists()) {
					return new SendEmailResult(-1, "邮件主体中的附件不存在:" + toUser.getFile().getAbsolutePath());
				}
				messageBodyPart = new MimeBodyPart();
				messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(toUser.getFile())));
				messageBodyPart.setFileName(MimeUtility.encodeText(toUser.getFileName()));
				multipart.addBodyPart(messageBodyPart);
			}
			// 3.5设置邮件主体内容
			message.setContent(multipart);
			// 4.发送邮件
			ts.sendMessage(message, message.getAllRecipients());
			ts.close();// 若系统中邮件发送较多，该对象可静态化不再关闭

			return new SendEmailResult(0, "邮件发送成功。");
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
			return new SendEmailResult(-1, "发送邮件开启SSL加密时异常。");
		} catch (MessagingException e) {
			e.printStackTrace();
			return new SendEmailResult(-1, "发送邮件的内容异常。");
		} catch (Exception e) {
			e.printStackTrace();
			return new SendEmailResult(-1, "发送邮件未知异常。");
		}

	}

	/**
	 * 发送邮件返回的对象
	 */
	public static class SendEmailResult {
		private Integer code;
		private String msg;

		public Integer getCode() {
			return code;
		}

		public String getMsg() {
			return msg;
		}

		public SendEmailResult(Integer code, String msg) {
			this.code = code;
			this.msg = msg;
		}

		@Override
		public String toString() {
			return "SendEmailResult{" + "code=" + code + ", msg='" + msg + '\'' + '}';
		}
	}

	/**
	 * 发送人邮件对象
	 */
	public static class MailFromUserModel {
		private String host;
		private String user;
		private String passWord;

		public String getHost() {
			return host;
		}

		public String getUser() {
			return user;
		}

		public String getPassWord() {
			return passWord;
		}

		public MailFromUserModel(String host, String user, String passWord) {
			this.host = host;
			this.user = user;
			this.passWord = passWord;
		}
	}

	/**
	 * 接收邮件对象
	 */
	public static class MailToUserModel {
		private String user;
		private String subject;
		private String text;
		private File file;
		private String fileName;

		public String getUser() {
			return user;
		}

		public String getSubject() {
			return subject;
		}

		public String getText() {
			return text;
		}

		public File getFile() {
			return file;
		}

		public String getFileName() {
			return fileName;
		}

		public MailToUserModel(String user, String subject, String text, File file, String fileName) {
			super();
			this.user = user;
			this.subject = subject;
			this.text = text;
			this.file = file;
			this.fileName = fileName;
		}
	}
}
